/*
 * Copyright 2011 Alex Paréj.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mkdir.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.mkdir.util.FajlUtil;
import org.mkdir.util.Konstans;

/**
 *
 * @author Alex Paréj
 */
@Entity
@Table(name = "DOKUMENTUMOK")
public class Dokumentum extends AbstractEntitiy<Integer> implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "DOKUMENTUM_ID")
    private Integer id;
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LETREHOZO", nullable = false)
    private Felhasznalo letrehozo;
    @Size(max = 100)
    @Column(name = "NEV")
    @Basic(optional = false)
    @NotNull
    private String nev;
    @Lob
    @NotNull
    @Basic(fetch = FetchType.LAZY, optional = false)
    @Column(name = "DOKUMENTUM")
    private byte[] dokumentum;
    @Size(max = 255)
    @Column(name = "MEGJEGYZES")
    private String megjegyzes;
    @Basic(optional = false)
    @NotNull
    @Column(name = "LETREHOZVA")
    @Temporal(TemporalType.TIMESTAMP)
    private Date letrehozva;
    @Column(name = "TOROLVE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date torolve;
    @Basic(optional = false)
    @NotNull
    @Column(name = "LOMTAR")
    private Character lomtar;
    @Basic(optional = false)
    @NotNull
    @Column(name = "MERET")
    private Long meret;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "FE_DOK_MEGOSZTASOK",
    joinColumns = {
        @JoinColumn(name = "DOKUMENTUM_ID", referencedColumnName = "DOKUMENTUM_ID")},
    inverseJoinColumns = {
        @JoinColumn(name = "FELHASZNALO_ID", referencedColumnName = "FELHASZNALO_ID")})
    private List<Felhasznalo> felhasznalok;
    @Column(name = "PUBLIKUS")
    private Character publikus;
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "DOK_FE_KEDVENCEK",
    joinColumns = {
        @JoinColumn(name = "DOKUMENTUM_ID", referencedColumnName = "DOKUMENTUM_ID")},
    inverseJoinColumns = {
        @JoinColumn(name = "FELHASZNALO_ID", referencedColumnName = "FELHASZNALO_ID")})
    private List<Felhasznalo> kedvencnekJeloltek;
    @Column(name = "MODOSITVA")
    @Temporal(TemporalType.TIMESTAMP)
    private Date modositva;

    public Dokumentum() {
    }

    public Dokumentum(Integer id) {
        this.id = id;
    }

    public byte[] getDokumentum() {
        return dokumentum;
    }

    public void setDokumentum(byte[] dokumentum) {
        this.dokumentum = dokumentum;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public void setId(Integer id) {
        this.id = id;
    }

    public Felhasznalo getLetrehozo() {
        return letrehozo;
    }

    public void setLetrehozo(Felhasznalo letrehozo) {
        this.letrehozo = letrehozo;
    }

    public Date getLetrehozva() {
        return letrehozva;
    }

    public void setLetrehozva(Date letrehozva) {
        this.letrehozva = letrehozva;
    }

    public String getMegjegyzes() {
        return megjegyzes;
    }

    public void setMegjegyzes(String megjegyzes) {
        this.megjegyzes = megjegyzes;
    }

    public String getNev() {
        return nev;
    }

    public void setNev(String nev) {
        this.nev = nev;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Dokumentum)) {
            return false;
        }
        Dokumentum other = (Dokumentum) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return nev;
    }

    public String getKiterjesztes() {
        return FajlUtil.getKiterjesztes(nev);
    }

    public void setLomtar(boolean lomtar) {
        if (lomtar) {
            this.lomtar = Konstans.IGEN;
        } else {
            this.lomtar = Konstans.NEM;
        }
    }

    public boolean isLomtar() {
        if (lomtar != null && lomtar.equals(Konstans.IGEN)) {
            return true;
        }
        return false;
    }

    public Date getTorolve() {
        return torolve;
    }

    public void setTorolve(Date torolve) {
        this.torolve = torolve;
    }

    public Long getMeret() {
        return meret;
    }

    public void setMeret(Long meret) {
        this.meret = meret;
    }

    public List<Felhasznalo> getFelhasznalok() {
        if (felhasznalok == null) {
            felhasznalok = new ArrayList<Felhasznalo>();
        }
        return felhasznalok;
    }

    public boolean isPublikus() {
        if (publikus != null && publikus.equals(Konstans.IGEN)) {
            return true;
        }
        return false;
    }

    public void setPublikus(boolean publik) {
        if (publik) {
            publikus = Konstans.IGEN;
        } else {
            publikus = Konstans.NEM;
        }
    }

    private List<Felhasznalo> getKedvencnekJeloltek() {
        if (kedvencnekJeloltek == null) {
            kedvencnekJeloltek = new ArrayList<Felhasznalo>();
        }
        return kedvencnekJeloltek;
    }

    public boolean isKedvenc(Felhasznalo felhasznalo) {
        return getKedvencnekJeloltek().contains(felhasznalo);
    }

    public void setKedvenc(Felhasznalo felhasznalo, boolean kedvenc) {
        if (kedvenc) {
            getKedvencnekJeloltek().add(felhasznalo);
        } else {
            getKedvencnekJeloltek().remove(felhasznalo);
        }

    }

    public Date getModositva() {
        return modositva;
    }

    public void setModositva(Date modositva) {
        this.modositva = modositva;
    }
}
